﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LAVAA.DAL;
using LAVAA.WEB.Models;

namespace LAVAA.WEB.Controllers
{
    //[Authorize(Roles = "Admin")]
    public class UserController : Controller
    {
        CustomRoleProvider customRoleProvider = new CustomRoleProvider();

        //
        // GET: /User/

        public ActionResult Index()
        {
            return View(new LAVAA.BL.Control.UserFacade().FindUsers(null, null, null, null));
        }

        //
        // GET: /User/Create

        //[Authorize(Roles = "Test")]
        public ActionResult Create()
        {
            if (Request.IsAjaxRequest())
                return PartialView("_Create", new LAVAA.WEB.Models.UserView());
            return View(new LAVAA.WEB.Models.UserView());
           // if (customRoleProvider.GetRolesForUser(User.Identity.Name).Contains("Test"))
                //return View(new LAVAA.WEB.Models.UserView());
           // else
          //  {
         //       TempData["ErrorMessage"] = "Access is denied";
            //    return RedirectToAction("Index");
           // }
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(UserView userView)
        {
            LAVAA.BL.Control.UserFacade userFacade = new LAVAA.BL.Control.UserFacade();
            if (ModelState.IsValid)
            {
                try
                {
                    userFacade.AddUser(userView.User, userView.SelectedUserGroup);
                    if (Request.IsAjaxRequest())
                        return Json(new { success = true });
                    return RedirectToAction("Index");
                }
                catch (Exception e)
                {
                    ModelState.AddModelError(string.Empty, e.Message);
                }
            }
            if (Request.IsAjaxRequest())
                return PartialView("_Create", userView);
            return View(userView);
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(Guid id)
        {
            LAVAA.BL.Control.UserFacade userFacade = new LAVAA.BL.Control.UserFacade();
            UserView userView = new UserView();
            User user = userFacade.FindUserById(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            userView.User = user;
            userView.SelectedUserGroup = user.UserGroup.Id;
            return View(userView);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(UserView userView)
        {
            LAVAA.BL.Control.UserFacade userFacade = new LAVAA.BL.Control.UserFacade();
            if (ModelState.IsValid)
            {
                userFacade.UpdateUser(userView.User, userView.SelectedUserGroup);
                return RedirectToAction("Index");
            }
            return View(userView);
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(Guid id)
        {
            LAVAA.BL.Control.UserFacade userFacade = new LAVAA.BL.Control.UserFacade();
            userFacade.DeleteUser(id);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        public ActionResult Search(string username, string firstname, string lastname, string group)
        {
            return View("Index", new LAVAA.BL.Control.UserFacade().FindUsers(username, firstname, lastname, group));
        }
    }
}